package com.mumulx.check.service.util;

import com.mumulx.check.model.SubmitRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Component("helpDev")
@Slf4j
public class HelpDev {

    //window本地
    //定义上传prt保存的本地文件夹
    public static final String FILEUPLADURL = "C:\\workplace\\IDEA\\MyProject\\fileUpload";
    /*UG环境*/
    public static final String CHECKMATEPATH = "C:\\Program Files\\Siemens\\NX 12.0\\UGII";
    /*获取文件预览图的exe文件位置*/
    public static final String JPGEXEPATH = "C:\\Users\\26069\\Desktop\\nx项目\\需求分析\\ImgDos";
    //本次检测预计的时间：一个文件假设需要5s
    public static final int TESTFILETIME = 5;

    //部署在linux
    //private static final String UPLADMIMGSRC = "/app/tools";

    //MD5加密
    public String md5Encode(byte[] input) {
        return DigestUtils.md5Hex(input);//byte[]--->String
    }



    /**
     * 验证手机号是否合法
     *
     * @return
     */
    public static boolean isMobileNO(String mobile) {
        if (mobile.length() != 11) {
            return false;
        } else {
            /**
             * 移动号段正则表达式
             */
            String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
            /**
             * 联通号段正则表达式
             */
            String pat2 = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
            /**
             * 电信号段正则表达式
             */
            String pat3 = "^((133)|(153)|(177)|(18[0,1,9])|(149))\\d{8}$";
            /**
             * 虚拟运营商正则表达式
             */
            String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";

            Pattern pattern1 = Pattern.compile(pat1);
            Matcher match1 = pattern1.matcher(mobile);
            boolean isMatch1 = match1.matches();
            if (isMatch1) {
                return true;
            }
            Pattern pattern2 = Pattern.compile(pat2);
            Matcher match2 = pattern2.matcher(mobile);
            boolean isMatch2 = match2.matches();
            if (isMatch2) {
                return true;
            }
            Pattern pattern3 = Pattern.compile(pat3);
            Matcher match3 = pattern3.matcher(mobile);
            boolean isMatch3 = match3.matches();
            if (isMatch3) {
                return true;
            }
            Pattern pattern4 = Pattern.compile(pat4);
            Matcher match4 = pattern4.matcher(mobile);
            boolean isMatch4 = match4.matches();
            if (isMatch4) {
                return true;
            }
            return false;
        }
    }


    // 批量文件压缩成zip包
    public File zipFiles(List<File> fileList, String zipPath, String zipName) throws IOException {
        // 如果被压缩文件中有重复，会重命名
        Map<String, String> namePathMap = getTransferName(fileList);
        File zipPathFile = new File(zipPath);
        // 文件夹不存在则创建
        if (!zipPathFile.exists()) {
            zipPathFile.mkdirs();
        }
        File zipFile = new File(zipPath + File.separator + zipName);
        if (!zipFile.exists()) {
            zipFile.createNewFile();
        }
        ZipOutputStream zos = null;
        BufferedInputStream bis = null;
        try {
            // 存放的目标文件
            zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile.getPath())));
            Set<String> keySet = namePathMap.keySet();
            ZipEntry zipEntry = null;
            for (String key : keySet) { // key是文件名，value是path
                // 指定源文件
                File sourceFile = new File(namePathMap.get(key));
                // 创建ZIP实体，并添加进压缩包,指定压缩文件中的文件名
                zipEntry = new ZipEntry(key);
                zos.putNextEntry(zipEntry);
                // 读取待压缩的文件并写进压缩包里
                bis = new BufferedInputStream(new BufferedInputStream(new FileInputStream(sourceFile), 1024 * 10));
                byte[] bufs = new byte[1024 * 10];
                int read = 0;
                while ((read = (bis.read(bufs, 0, 1024 * 10))) != -1) {
                    zos.write(bufs, 0, read);
                }
                if (bis != null) {
                    bis.close();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
// 关闭流
            if (bis != null) {
                bis.close();
            }
            if (null != zos) {
                zos.close();
            }
        }
        return zipFile;
    }


    // 计算压缩包如果已存在重复的名称，则在重复文件后面跟上数字 如: 文件(1).doc,文件(2).doc
    public Map<String, String> getTransferName(List<File> fileList) {
        if (fileList == null || fileList.size() == 0) {
            return new HashMap<String, String>();
        }
        // key存放文件名，value存放path
        Map<String, String> fileNameMap = new HashMap<>();
        List<String> fileNames = new ArrayList<>();
        for (File file : fileList) {
            // 获取文件名
            String fileName = file.getName();
            int count = 0;

            for (String name : fileNames) {
                if (name != null && name.equals(fileName)) {
                    count++;
                }
            }
            fileNames.add(fileName);
            if (count > 0) {
                int lastIndex = fileName.lastIndexOf('.');
                String name = fileName.substring(0, lastIndex);
                String type = fileName.substring(lastIndex + 1, fileName.length());
                fileName = new StringBuilder().append(name).append("(").append(count).append(")").append(".")
                        .append(type).toString();
                fileNameMap.put(fileName, file.getPath());
            } else {
                fileNameMap.put(fileName, file.getPath());
            }
        }
        return fileNameMap;
    }


    
}
